package ncorps.newton;

import ncorps.commun.ConditionInitiale;
import static java.lang.Math.*;

public class ForcesNewton {

    private double[][] gcoord;
    private int _N;
    private double _dt;
    private double[][] fforces;

    ForcesNewton(ConditionInitiale CI, double dt) {
        _dt = dt;
        _N = CI.get_N();
        gcoord = CI.getGcoord();
        calcul();
    }

    private void calcul() {
        double[] f = new double[2];
        fforces = new double[_N][2];
        for (int j = 0; j < _N; j++) {
            f = totalForcesSur(j);
            fforces[j][0] = f[0];
            fforces[j][1] = f[1];
            for (int k = 0; k <= 2; k++) {
                gcoord[j][1] = (_dt * _dt / 2) * f[0] + (gcoord[j][3]) * _dt + gcoord[j][1];
                gcoord[j][2] = (_dt * _dt / 2) * f[1] + (gcoord[j][4]) * _dt + gcoord[j][2];
                gcoord[j][3] = f[0] * _dt + gcoord[j][3];
                gcoord[j][4] = f[1] * _dt + gcoord[j][4];
            }
        }
    }

    private double[] totalForcesSur(int j) {
        double[] f = new double[2];
        double dist2;
        double xj = gcoord[j][1];
        double yj = gcoord[j][2];
        double D;

        for (int i = 0; i < _N; i++) {
            if (i != j) {
                dist2 = ((gcoord[i][1] - xj) * (gcoord[i][1] - xj)) + ((gcoord[i][2] - yj) * (gcoord[i][2] - yj));
                D = pow(dist2, 1.5);
                f[0] += gcoord[i][0] * ((gcoord[i][1] - xj) / D); //accelerations en x y pour j due aux i!=j
                f[1] += gcoord[i][0] * ((gcoord[i][2] - yj) / D);
            }
        }
        return f;
    }

    public double[][] getGcoord() {
        return gcoord;
    }

    public double[][] getFforces() {
        return fforces;
    }

}
